package com.mercator.client.graph;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import org.vaadin.gwtgraphics.client.DrawingArea;
import org.vaadin.gwtgraphics.client.Shape;
import org.vaadin.gwtgraphics.client.animation.Animate;
import org.vaadin.gwtgraphics.client.shape.Text;



public abstract class StringNode<T extends Comparable<? super T>, N extends StringNode<T,N,S>, S extends Shape> extends GNode<T,N,S> {
//	private static final double COS45 = Math.cos(45);
	
	protected boolean first = true;
	protected Text text;
	private DrawingArea canvas;
	//private Image image;
	//private StringNode selfref;
	public StringNode(DrawingArea canvas, T element) {
		super(canvas,element);
		this.canvas = canvas; 
		text = new Text(getThis().getCenterX(), getThis().getCenterY(),  element.toString());
		int textlen = text.getTextWidth();
		int textHeight = text.getTextHeight();
		text = new Text(getThis().getCenterX()-textlen/2, getThis().getCenterY() -textHeight/2,  element.toString());
		 
		reps.addClickHandler(new SClickHandler());
		// selfref =this;
		canvas.addMouseDownHandler(new MDClickHandler(getThis())); 
		//sinkEvents(Event.MOUSEEVENTS);
			
	}
	
	public StringNode(T element){
		super(element);
	}
	
	@Override
	public void animate(){
		super.animate();
		if(first){
		text = new Text(getThis().getCenterX() -text.getTextWidth()/2, getThis().getCenterY()- text.getTextHeight()/2,  text.getText());
		text.setFillColor("black");
		reps.add(text);	 
		//getThis().setSize(text.getTextWidth(), text.getTextHeight() + 10);
		text.setX(getCenterX() - text.getOffsetWidth()/2);
		text.setY(getCenterY());
		canvas.bringToFront(reps);
		first = false;
		}
		else{
		new Animate(text,"x",text.getX(),getThis().getTargetCenterX() -text.getTextWidth()/2,speed).start();
    	new Animate(text,"y",text.getY(),getThis().getTargetCenterY() - text.getTextHeight()/2,speed).start();	
		}
	}
	
	public boolean equals(N sn){
		return this.element.equals(sn.element);
		
	}
	
	public int hashCode(){
		return this.element.hashCode();
	}
	
	public void unselect(){
		super.unselect();
		text.setStrokeColor("black");
		selected = false;
	}
	
	public class SClickHandler implements ClickHandler{

		@Override
		public void onClick(ClickEvent event) {
			select();
			canvas.bringToFront(reps);
			rep.setFillColor("Red");
			text.setStrokeColor("blue");
			selected = true;
		}
	
	}
	@Override
	protected void select(){
		text.setStrokeColor("blue");
		selected = true;
	}
	
	
	public class MDClickHandler implements MouseDownHandler{
		private N self;
		
		public MDClickHandler(N self ){
			this.self = getThis();
		}
		@Override
		public void onMouseDown(MouseDownEvent event) {
			int x = event.getClientX();
			int y = event.getClientY();
/*		if (selected && inRadius(event.getClientX(),event.getClientY())){
				self.setPos(x, y);
			}
			else*/
				if(selected)
					getThis().moveTo(x, y);
	}
		
		private boolean inRadius(int x, int y){
			//System.out.println("diffx:"+ (self.getY()-y) + "diffy: " + (self.getX() - x)  + "\ndist: " +Math.sqrt(Math.pow((self.getX() - x),2.0) + Math.pow((self.getY()-y),2)) + "\nradius: " + self.radius);
			return Math.sqrt(Math.pow((self.getX() - x),2) + Math.pow((self.getY()-y),2)) < self.radius;
			
		}
		
		
	}

	
	protected abstract void setSize(int x, int y);
	
	
	 

}
